package com.yz.group5.med_booking.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yz.group5.med_booking.entity.Doctor;
import com.yz.group5.med_booking.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin // 允许跨域访问
@RestController // 标记当前类是一个控制器类，返回结果为JSON数据结构
@RequestMapping("/doctor") // 统一前缀，前端访问 /doctor开头的接口
public class DoctorController {

    @Autowired // 注入Service
    private DoctorService doctorService;

    /**
     * 获取医生列表（分页+搜索）
     */
    @GetMapping("/list")
    public Map<String, Object> listDoctors(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer deptId,
            @RequestParam(required = false) String title) {

        // 构建查询条件
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }
        if (deptId != null) {
            queryWrapper.eq("dept_id", deptId);
        }
        if (title != null && !title.isEmpty()) {
            queryWrapper.like("title", title);
        }

        // 分页查询
        Page<Doctor> doctorPage = new Page<>(page, pageSize);
        doctorService.page(doctorPage, queryWrapper);

        // 返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("records", doctorPage.getRecords());
        result.put("total", doctorPage.getTotal());
        return result;
    }

    /**
     * 根据ID获取医生信息
     */
    @GetMapping("/{id}")
    public Doctor getDoctorById(@PathVariable Integer id) {
        return doctorService.getById(id);
    }

    /**
     * 添加医生
     */
    @PostMapping
    public Map<String, Object> addDoctor(@RequestBody Doctor doctor) {
        Map<String, Object> result = new HashMap<>();
        boolean success = doctorService.save(doctor);
        result.put("success", success);
        result.put("message", success ? "添加成功" : "添加失败");
        return result;
    }

    /**
     * 更新医生信息
     */
    @PutMapping("/{id}")
    public Map<String, Object> updateDoctor(@PathVariable Integer id, @RequestBody Doctor doctor) {
        Map<String, Object> result = new HashMap<>();

        // 确保ID一致
        doctor.setId(id);

        boolean success = doctorService.updateById(doctor);
        result.put("success", success);
        result.put("message", success ? "更新成功" : "更新失败");
        return result;
    }

    /**
     * 删除医生
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteDoctor(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        boolean success = doctorService.removeById(id);
        result.put("success", success);
        result.put("message", success ? "删除成功" : "删除失败");
        return result;
    }
}